/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx.db;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Properties;

/**
 * <p>Project: jdemo202203 - DbTypeDemo
 * <p>Powered by webrx On 2022-11-02 11:30:37
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class DbTypeDemo {
    Connection conn = null;

    @Test @DisplayName("java 文件 mysql longblob")
    void t4(){
        //插入数据
        String filename = "f1.jpg";
        File file = new File("c:/",filename);
        long filesize = file.length();

        try{
            String sql = "insert into t_files value(uuid_short(),?,?,?)";
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1,filename);
            pst.setLong(2,filesize);
            InputStream is = new FileInputStream(file);
            pst.setBlob(3,is);

            //int rows = pst.executeUpdate();
        }catch(Exception e){
            e.printStackTrace();
        }

        //读取数据库中的文件(图像)
        try{
            PreparedStatement pt = conn.prepareStatement("select filename,file from t_files where id = ?");
            long id = 27974167873716224L;
            pt.setLong(1,id);
            ResultSet rs = pt.executeQuery();
            rs.next();
            System.out.println(rs.getString(1));

            //InputStream is = rs.getBlob(2).getBinaryStream();
            //byte[] buf = is.readAllBytes();

        }catch(Exception e){
            e.printStackTrace();
        }


    }


    @Test
    void t3(){
        try{
            String sql = "select * from t";
            PreparedStatement pst = conn.prepareStatement(sql);
            ResultSet rs = pst.executeQuery();
            rs.next();
            //System.out.println(rs.getLong(1));
            //System.out.printf("%tF %<tT%n",rs.getLong("t1"));
            //System.out.println(rs.getDate(2).toLocalDate().format(DateTimeFormatter.ofPattern("yyyy年MM月dd日")));
            System.out.println(rs.getTimestamp(3).toLocalDateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            System.out.println(rs.getTimestamp(4).toLocalDateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

            System.out.println(rs.getTime(5).toLocalTime());
            System.out.printf("%.5f",rs.getDouble(6));

        }catch(Exception e){
            e.printStackTrace();
        }
    }


    @Test
    void t2(){
        try{
            String sql = "insert into t value(?,?,?,?,?,?)";
            PreparedStatement pst = conn.prepareStatement(sql);

            //java时间戳
            pst.setLong(1,System.currentTimeMillis());

            //1985-10-02
            pst.setDate(2, Date.valueOf(LocalDate.of(1985,10,2)));
            //pst.setString(2,"1985-10-15");

            pst.setTimestamp(3, Timestamp.valueOf(LocalDateTime.now()));

            pst.setTimestamp(4, Timestamp.valueOf(LocalDateTime.now()));


            pst.setTime(5, Time.valueOf(LocalTime.now()));

            pst.setDouble(6,10d);

            int rows = pst.executeUpdate();
            System.out.println(rows);

        }catch(Exception e){
            e.printStackTrace();
        }
    }


    @Test
    void t1(){
        try{
            PreparedStatement pst = conn.prepareStatement("select unix_timestamp(),now()");
            ResultSet rs = pst.executeQuery();
            rs.next();
            System.out.println(rs.getLong(1));
            System.out.println(rs.getTimestamp(2));
            System.out.println(System.currentTimeMillis());
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    @BeforeEach
    void connection() {
        try {
            InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties");
            Properties prop = new Properties();
            prop.load(is);
            String driver = prop.getProperty("db.driver", "com.mysql.cj.jdbc.Driver");
            String url = prop.getProperty("db.url", "jdbc:mysql:/");
            String user = prop.getProperty("db.username", "root");
            String password = prop.getProperty("db.password", "");

            Class.forName(driver);
            conn = DriverManager.getConnection(url, user, password);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @AfterEach
    void close() {
        try {
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
